import { Http } from '@/@nasty/Axios';
import { util } from '@/@nasty/Util';
import { SelectUserModel } from '@/@nasty/components/SelectUserModel';
import Upload from '@/@nasty/components/Upload';
import { PlusOutlined } from '@ant-design/icons';
import { ActionType, ModalForm, ProFormInstance, ProFormItem, ProFormSelect, ProFormText, ProFormTextArea } from '@ant-design/pro-components';
import { FormattedMessage, useIntl, useRequest } from '@umijs/max';
import { Button, Checkbox, Divider, FormInstance, message } from 'antd';
import React from 'react';
import { FC, cloneElement, forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react';
import { SelectRoleModel } from '@/@nasty/components/SelectRoleModel';
import { SelectDepartmentModel } from '@/@nasty/components/SelectDepartmentModel';
import { candidateModes } from '@/@nasty/components/Common';


interface NodeModelProps {
    onChange?: (v: any) => boolean;
}

const AddNodeModel = forwardRef((props: NodeModelProps, ref) => {

    const [isOpen, setIsOpen] = useState(false);
    const [candidateMode, setCandidateMode] = useState(1);
    const [messageApi, contextHolder] = message.useMessage();
    const formRef = React.useRef<ProFormInstance>(null);

    /**
     * @en-US International configuration
     * @zh-CN 国际化配置
     * */
    const intl = useIntl();

    // 定义可以被外部调用的 open 函数
    useImperativeHandle(ref, () => ({
        open: () => {
            setIsOpen(true);
            setCandidateMode(1);
        },
        close: () => {
            setIsOpen(false);
        }
    }));


    return (
        <>
            {contextHolder}
            <ModalForm
                title={"新增节点"}
                width="552px"
                formRef={formRef}
                open={isOpen}
                modalProps={{
                    onCancel: () => { setIsOpen(false); },
                }}
                onFinish={async (value) => {
                    if (props.onChange) {
                        let res = props.onChange(value);
                        if (res) {
                            setIsOpen(false);
                        } else {
                            messageApi.warning("节点重复");
                        }
                    } else {
                        setIsOpen(false);
                    }
                }}
            >
                <ProFormText label="节点名称" name="Name" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "节点名称必填",
                        },
                    ]}

                />

                <ProFormText label="节点编码" name="Code" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "节点编码必填，且不能重复",
                        },
                    ]}
                />

                <ProFormSelect label="候选模式" name="CandidateMode" width="xl"
                    options={candidateModes}
                    onChange={(v: number) => { setCandidateMode(v); }}
                    mode="single"
                    rules={[
                        {
                            required: true,
                            message: "候选模式编码必填",
                        },
                    ]}
                />


                {candidateMode == 2 ? <SelectUserModel
                    onSelect={(row: any) => { formRef.current?.setFieldValue("PreUserIds", row); }}
                    defaultValue={() => { return formRef.current?.getFieldValue("PreUserIds") || []; }}
                    multiple={true}
                    trigger={
                        <div>
                            <ProFormSelect
                                label="预选用户"
                                name="PreUserIds"
                                mode="multiple"
                                rules={[{ required: false }]}
                                fieldProps={{ open: false, }}>
                            </ProFormSelect>
                        </div>
                    } /> : null}

                {candidateMode == 3 ? <SelectRoleModel
                    onSelect={(row: any) => { formRef.current?.setFieldValue("RoleIds", row); }}
                    defaultValue={() => { return formRef.current?.getFieldValue("RoleIds") || []; }}
                    multiple={true}
                    trigger={
                        <div>
                            <ProFormSelect
                                label="角色"
                                name="RoleIds"
                                mode="multiple"
                                rules={[{ required: false }]}
                                fieldProps={{ open: false, }}>
                            </ProFormSelect>
                        </div>
                    } /> : null}

                {candidateMode == 4 ? <SelectDepartmentModel
                    onSelect={(row: any) => { formRef.current?.setFieldValue("DepartmentIds", row); }}
                    defaultValue={() => { return formRef.current?.getFieldValue("DepartmentIds") || []; }}
                    multiple={true}
                    trigger={
                        <div>
                            <ProFormSelect
                                label="部门"
                                name="DepartmentIds"
                                mode="multiple"
                                rules={[{ required: false }]}
                                fieldProps={{ open: false, }}>
                            </ProFormSelect>
                        </div>
                    } /> : null}
            </ModalForm>
        </>
    );
});


const UpdateNodeModel = forwardRef((props: NodeModelProps, ref) => {

    const [isOpen, setIsOpen] = useState(false);
    const [messageApi, contextHolder] = message.useMessage();
    const [candidateMode, setCandidateMode] = useState(1);
    /**
     * @en-US International configuration
     * @zh-CN 国际化配置
     * */
    const intl = useIntl();

    const formRef = React.useRef<ProFormInstance>(null);

    // 定义可以被外部调用的 open 函数
    useImperativeHandle(ref, () => ({
        open: (v: any) => {
            setIsOpen(true);
            setCandidateMode(v.CandidateMode || 1);
            setTimeout(() => {
                formRef.current?.setFieldsValue(v);
            }, 100)
        },
        close: () => {
            setIsOpen(false);
        }
    }));


    return (
        <>
            {contextHolder}
            <ModalForm
                formRef={formRef}
                title={"编辑节点"}
                width="552px"
                open={isOpen}

                modalProps={{
                    onCancel: () => { setIsOpen(false); },
                }}
                onFinish={async (value) => {
                    if (props.onChange) {
                        let res = props.onChange(value);
                        if (res) {
                            setIsOpen(false);
                        } else {
                            messageApi.warning("节点重复");
                        }
                    } else {
                        setIsOpen(false);
                    }
                }}
            >

                <ProFormText label="节点编码" name="OriginalCode" width="xl" hidden={true} />
                <ProFormText label="节点X位置" name="X" width="xl" hidden={true} />
                <ProFormText label="节点Y位置" name="Y" width="xl" hidden={true} />

                <ProFormText label="节点名称" name="Name" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "节点名称必填",
                        },
                    ]}

                />

                <ProFormText label="节点编码" name="Code" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "节点编码必填，且不能重复",
                        },
                    ]}
                />



                <ProFormSelect label="候选模式" name="CandidateMode" width="xl"
                    options={candidateModes}
                    onChange={(v: number) => { setCandidateMode(v); }}
                    mode="single"
                    rules={[
                        {
                            required: true,
                            message: "候选模式编码必填",
                        },
                    ]}
                />

                {candidateMode == 2 ? <SelectUserModel
                    onSelect={(row: any) => { formRef.current?.setFieldValue("PreUserIds", row); }}
                    defaultValue={() => { return formRef.current?.getFieldValue("PreUserIds") || []; }}
                    multiple={true}
                    trigger={
                        <div>
                            <ProFormSelect
                                label="预选用户"
                                name="PreUserIds"
                                mode="multiple"
                                rules={[{ required: false }]}
                                fieldProps={{ open: false, }}>
                            </ProFormSelect>
                        </div>
                    } /> : null}

                {candidateMode == 3 ? <SelectRoleModel
                    onSelect={(row: any) => { formRef.current?.setFieldValue("RoleIds", row); }}
                    defaultValue={() => { return formRef.current?.getFieldValue("RoleIds") || []; }}
                    multiple={true}
                    trigger={
                        <div>
                            <ProFormSelect
                                label="角色"
                                name="RoleIds"
                                mode="multiple"
                                rules={[{ required: false }]}
                                fieldProps={{ open: false, }}>
                            </ProFormSelect>
                        </div>
                    } /> : null}

                {candidateMode == 4 ? <SelectDepartmentModel
                    onSelect={(row: any) => { formRef.current?.setFieldValue("DepartmentIds", row); }}
                    defaultValue={() => { return formRef.current?.getFieldValue("DepartmentIds") || []; }}
                    multiple={true}
                    trigger={
                        <div>
                            <ProFormSelect
                                label="部门"
                                name="DepartmentIds"
                                mode="multiple"
                                rules={[{ required: false }]}
                                fieldProps={{ open: false, }}>
                            </ProFormSelect>
                        </div>
                    } /> : null}
            </ModalForm>
        </>
    );
});


const RejectNodeModel = forwardRef((props: NodeModelProps, ref) => {

    const [isOpen, setIsOpen] = useState(false);
    const [nodes, setNodes] = useState<any[]>([]);
    const [messageApi, contextHolder] = message.useMessage();

    /**
     * @en-US International configuration
     * @zh-CN 国际化配置
     * */
    const intl = useIntl();

    const formRef = React.useRef<ProFormInstance>(null);

    // 定义可以被外部调用的 open 函数
    useImperativeHandle(ref, () => ({
        open: (v: any, nodes: any[]) => {
            setIsOpen(true);
            setNodes(nodes);
            setTimeout(() => {
                formRef.current?.setFieldsValue(v);
            }, 100)
        },
        close: () => {
            setIsOpen(false);
        }
    }));


    return (
        <>
            {contextHolder}
            <ModalForm
                formRef={formRef}
                title={"驳回节点"}
                width="552px"
                open={isOpen}

                modalProps={{
                    onCancel: () => { setIsOpen(false); },
                }}
                onFinish={async (value) => {
                    if (props.onChange) {
                        let res = props.onChange(value);
                        if (res) {
                            setIsOpen(false);
                        } else {
                            messageApi.warning("驳回节点编码不存在");
                        }
                    } else {
                        setIsOpen(false);
                    }
                }}
            >

                <ProFormText label="发起节点" name="Name" width="xl" disabled />
                <ProFormText label="发起节点" name="Code" width="xl" hidden={true} />

                {/*         <ProFormText label="驳回节点" name="RejectCode" width="xl"
                    rules={[
                        {
                            required: true,
                            message: "驳回节点编码必填",
                        },
                    ]}
                /> */}

                <ProFormSelect
                    label="驳回节点"
                    name="RejectCode"
                    width="xl"
                    mode="single"
                    options={nodes}
                    rules={[{ required: true, message: "驳回节点必选" }]}>
                </ProFormSelect>

            </ModalForm>
        </>
    );
});


export { AddNodeModel, UpdateNodeModel, RejectNodeModel }